using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Transactions;

namespace MTRC.ODMS.Web.SOM.DutySCRegister
{
    /// <summary>
    /// Author: SQB
    /// </summary>
    public partial class DutySCRegisterEntry : BasePage_SOM
    {
        /// <summary>
        /// 
        /// </summary>
        #region MenuID

        protected override string MenuID
        {
            get
            {
                return "M140702";
            }
        }

        #endregion

        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(SetLanguage_Click);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.TracePageChange = true;

            if (!IsPostBack)
            {
                this.btnSave.Attributes.Add("onclick", "return btnSaveClick('" + this.GetMessage("SOM_ShiftMandatory") + "')");
                if (this.Request["StationEventID"] == null)
                {
                    StationEventID = -1;
                }
                else
                {
                    StationEventID = int.Parse(this.Request["StationEventID"].ToString());
                }

                SetLanguage();
                SetPageLoad();

                //added by Nathan 2015-08-20
                if (Request.QueryString["isBack"] != null && Request.QueryString["isBack"].Trim() != string.Empty && Convert.ToInt32(Request.QueryString["isBack"].ToString()) > 0)
                {
                    btnBack.Enabled = true;
                }
                else
                {
                    btnBack.Enabled = false;
                }
                //End added by Nathan 2015-08-20
            }
            else
            {
                //this.IsServerChanged = true;
            }

            SetFunctionItem();
        }

        #endregion


        private void SetFunctionItem()
        {
            if (DAL.Security.User.CanFunctionItemAccess("M140702_F1", this.ODMUser.UserProfileID, this.MenuID))
            {
                this.btnNew.Enabled = false;
                this.btnSave.Enabled = false;
                this.btnDelete.Enabled = false;
            }
        }
        #region SetLanguage

        void SetLanguage_Click(object sender, EventArgs e)
        {
            SetLanguage();
            SetPageLoad(); //huangyao 20090220: fixed TD 1172
            SetFunctionItem();
        }

        private void SetPageLoad()
        {
            DateTime dtNow;
            DateTime eventTime;

            if (StationEventID == -1)
            {
                dtNow = MTRC.ODMS.DAL.COM.Util.GetCurrentSystemTime();
                this.txtRegDate.Text = FormatDateTime(dtNow, false);
                this.txtRegTime.Text = dtNow.ToString("HH") + ":" + dtNow.ToString("mm");

                this.btnDelete.Enabled = false;
                this.lblRegNoValue.Text = "";

                this.imgStatus.ImageUrl = "../../Images/SOM/EventStatus_OS.gif";
                this.imgStatus.ToolTip = this.GetLabel("SOM_OS");
                //Fill StaffID, StaffName, Title with login User
                DataTable dtOneStaff = MTRC.ODMS.DAL.SOM.CodeSetup.CdStaffInfo.GetOneStaffInfoByStaffInfoID(this.ODMUser.StaffInfoId);
                if (dtOneStaff.Rows.Count <= 0)
                {
                    this.lblStaffIDValue.Text = "";
                    this.lblStaffNameValue.Text = "";
                    this.lblTitleValue.Text = "";
                    this.Alert(this.GetMessage("SOM_WarningNotMTRStaff"));
                    string script = "<script language=javascript>function btnChange_Click(){window.location.href='DutySCRegisterEnquiry.aspx'} window.setTimeout(btnChange_Click,0);" + "</script>";
                    this.Page.RegisterStartupScript("aaa", script);
                    return;
                }
                this.lblStaffIDValue.Text = dtOneStaff.Rows[0]["StaffID"].ToString();
                this.lblStaffNameValue.Text = dtOneStaff.Rows[0]["StaffName"].ToString();
                this.lblTitleValue.Text = dtOneStaff.Rows[0]["JobTitle"].ToString();
            }
            else
            {
                string stationEventStatus;
                DataTable dtOneStationEvent = MTRC.ODMS.DAL.SOM.Event.StationEventSCBookOnDa.GetDutySCRegOne(this.LangCode, StationEventID);

                this.lblRegNoValue.Text = dtOneStationEvent.Rows[0]["EventNo"].ToString();
                stationEventStatus = dtOneStationEvent.Rows[0]["EventStatusID"].ToString();
                this.hdnDSCEventStatus.Value = stationEventStatus;

                //Fill Registry Time
                //if (dtStationAmendTime.Rows.Count <= 0)
                //{
                eventTime = DateTime.Parse(dtOneStationEvent.Rows[0]["EventTime"].ToString());
                this.txtRegDate.Text = FormatDateTime(eventTime, false);
                this.txtRegTime.Text = eventTime.ToString("HH") + ":" + eventTime.ToString("mm");
                //}
                //else
                //{
                //    eventTime = DateTime.Parse(dtStationAmendTime.Rows[0]["OriginalEventTime"].ToString());
                //    this.txtRegDate.Text = FormatDateTime(eventTime,false);
                //    this.txtRegTime.Text = eventTime.ToString("HH") + ";" + eventTime.ToString("mm");
                //}

                //add on 2008.12.12 by hzx "background-color: inherit;"
                if (dtOneStationEvent.Rows[0]["HasAmendArriveTimeCount"].ToString() != "0")
                {
                    string strColor = MTRC.ODMS.DAL.CommonData.GetStrVal("SOM_AmendedEventTime_Color").ToString();
                    btnRegDateTime.Attributes.Add("style", "BACKGROUND-COLOR:" + strColor);
                }

                //Fill Shift
                if (MTRC.ODMS.DAL.COM.Util.StringFromDB(dtOneStationEvent.Rows[0]["ShiftID"]) == string.Empty)
                {
                    this.dllShift.SelectedIndex = 0;
                }
                else
                {
                    ////this.dllShift.SelectedValue = dtOneStationEvent.Rows[0]["ShiftID"].ToString();
                    this.FindByValueAndSelect(this.dllShift, MTRC.ODMS.DAL.COM.Util.StringFromDB(dtOneStationEvent.Rows[0]["ShiftID"]));
                }

                //Fill Status
                this.lblStatusValue.Text = dtOneStationEvent.Rows[0]["EventStatusDesc"].ToString();

                if (stationEventStatus == "3")
                {
                    this.dllShift.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.imgStatus.ImageUrl = "../../Images/SOM/EventStatus_Void.gif";
                    this.imgStatus.ToolTip = this.GetLabel("SOM_Voided");
                }
                else
                {
                    if (stationEventStatus == "1")
                    {
                        this.imgStatus.ImageUrl = "../../Images/SOM/EventStatus_OS.gif";
                        this.imgStatus.ToolTip = this.GetLabel("SOM_OS");
                    }
                    else if (stationEventStatus == "2")
                    {
                        this.imgStatus.ImageUrl = "../../Images/SOM/EventStatus_COM.gif";
                        this.imgStatus.ToolTip = this.GetLabel("SOM_Completed");
                    }
                    this.btnDelete.Enabled = true;
                    this.btnDelete.Attributes.Add("onclick", "javascript:return btnDeleteClick('" + this.GetLabel("SOM_VoidSure") + "');");
                }

                ////BO-1436------no change current time, bkz need to modify the USP of [usp_SOM_DutySCReg_GetOne], need a LocationID field
                //int currentEntryLocationID = Convert.ToInt32(dtOneStationEvent.Rows[0]["LocationID"].ToString()); //BO-1436
                //if (this.CurrentStation!= currentEntryLocationID && this.StationEventID > 0)
                //{
                //    this.btnDelete.Enabled = false;
                //}
                ////------------------

                //this.btnRegDateTime.Attributes.Add("onclick", "javascript:window.showModalDialog('../Popup/SetRegEventTime.aspx?PageID=DutySCEntry&StationEventID=" + StationEventID + "',window,'dialogHeight:500px;dialogWidth:830px,center:yes,resizable:no,scroll:no;status:no;unadorne:yes'); return false;");
                this.btnSave.Enabled = false;
                this.dllShift.Enabled = false;


                //get StaffID, StaffName, Title with Request[StaffID]
                //DataTable dtOneStaff = MTRC.ODMS.DAL.SOM.CodeSetup.CdStaffInfo.getOneStaffInfoByByStaffID(this.Request["StaffID"].ToString());
                this.lblStaffIDValue.Text = dtOneStationEvent.Rows[0]["StaffID"].ToString();
                this.lblStaffNameValue.Text = dtOneStationEvent.Rows[0]["StaffName"].ToString();
                this.lblTitleValue.Text = dtOneStationEvent.Rows[0]["JobTitle"].ToString();
            }


            //this.btnNew.Attributes.Add("onclick", "javascript:if (CheckPageChange()){return true;}else{return false;}");
            //this.txtRegDate.Enabled = false;
            //this.txtRegTime.Enabled = false;
            FunctionItemSetting();

        }

        private void FunctionItemSetting()
        {
            if (DAL.Security.User.CanFunctionItemAccess("M140701_F3", this.ODMUser.UserProfileID, this.MenuID)) //readonly
            {
                this.btnNew.Enabled = false;
            }

            if (MTRC.ODMS.DAL.CommonData.GetCharVal("SOM_DutySC_CanAmendEventTime").ToUpper() == "Y")
            {
                this.btnRegDateTime.Visible = true;
                //if (!DAL.Security.User.CanFunctionItemAccess("M140702_F2", this.ODMUser.UserProfileID, this.MenuID)) //huangyao 20081022 commentted: consist the amend event time function
                if (DAL.Security.User.CanFunctionItemAccess("M140702_F1", this.ODMUser.UserProfileID, this.MenuID)
                    || !DAL.Security.User.CanFunctionItemAccess("M140702_F2", this.ODMUser.UserProfileID, this.MenuID)
                    || this.hdnDSCEventStatus.Value.Trim() == "2" || this.hdnDSCEventStatus.Value.Trim() == "3") //Event status is Completed and Void
                {
                    this.btnRegDateTime.Attributes.Add("onclick", "javascript:window.showModalDialog('../Popup/SetRegEventTime.aspx?PageID=DutySCEntry&ReadOnly=true&TimeType=1',window,'dialogHeight:500px;dialogWidth:830px;center:yes;resizable:yes;scroll:no;status:no;unadorne:yes;'); return false;");
                }
                else
                {
                    this.btnRegDateTime.Attributes.Add("onclick", "javascript:window.showModalDialog('../Popup/SetRegEventTime.aspx?PageID=DutySCEntry&ReadOnly=false&TimeType=1',window,'dialogHeight:500px;dialogWidth:830px;center:yes;resizable:yes;scroll:no;status:no;unadorne:yes'); return false;");
                }
            }
            else
            {
                this.btnRegDateTime.Visible = false;
            }
        }
        
        /// <summary>
        /// use com function
        /// </summary>
        /// <param name="ddlShift"></param>
        private void BindShift(DropDownList ddlShift)
        {
            ddlShift.Items.Clear();
            DataTable dt = MTRC.ODMS.DAL.SOM.CodeSetup.CDShift.GetAllShift(LangCode);
            //ddlShift.DataSource = dt;
            //ddlShift.DataValueField = MTRC.ODMS.Model.SOM.CodeSetup.CDShiftInfo.FIELDNAME_ShiftID;
            //ddlShift.DataTextField = MTRC.ODMS.Model.SOM.CodeSetup.CDShiftInfo.FIELDNAME_ShiftCode;
            //ddlShift.DataBind();
            //ddlShift.Items.Insert(0, LiBlank());
            //ddlShift.SelectedIndex = 0;

            BindListControl(this.dllShift, MTRC.ODMS.Model.SOM.CodeSetup.CDShiftInfo.FIELDNAME_ShiftID, MTRC.ODMS.Model.SOM.CodeSetup.CDShiftInfo.FIELDNAME_ShiftCode, dt, true, "");
            //this.FindByValueAndSelect(this.dllShift, );
            //this.dllShift.SelectedIndex = 0;
        }

        private void SetLanguage()
        {
            this.btnRegDateTime.Attributes["title"] = this.GetLabel("SOM_AmendEventTime"); //"Change Asset Loan Event Time"; --Add by HY 2009-03-18

            this.lblInfo.Text = this.GetLabel("SOM_Info");
            this.lblRegNo.Text = this.GetLabel("SOM_RegNo", true);
            this.lblStatus.Text = this.GetLabel("COM_Status", true);
            this.lblRegDateTime.Text = this.GetLabel("SOM_RegDateTime", true);
            this.lblShift.Text = this.GetLabel("SOM_Shift", true);
            this.lblStaffID.Text = this.GetLabel("SOM_StaffID", true);
            this.lblStaffName.Text = this.GetLabel("SOM_StaffName", true);
            this.lblTitle.Text = this.GetLabel("Com_Title", true);
            this.btnNew.Text = this.GetLabel("COM_Add");
            this.btnSave.Text = this.GetLabel("COM_Save");
            this.btnDelete.Text = this.GetLabel("SOM_VoidOperation");
            btnBack.Text = this.GetLabel("SOM_Back"); //Add Date 2015-08-21 Nathan begin U42.2

            BindShift(this.dllShift);
        }

        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int locationID;
            string staffID;
            string staffName;
            DateTime eventTime;

            // add by Kingkong Chan 20140730 for Code Merge (Changeset: 25619, 25747, 26099)
            //update by gordon 20151019  U247 Can not Test 
            //if (!DAL.SOM.Event.StationEventDa.IsLegalStation(this.ODMUser.StationID))  //add by kernel 2012-06-16 for no station issue 
            if (!DAL.SOM.Event.StationEventDa.IsLegalStation(this.ODMUser.LocationID))  //add by kernel 2012-06-16 for no station issue 
            {
                this.Alert(this.GetMessage("SOM_LegalStationCheck"));
                return;
            }

            //update by gordon 20151020 U247  Can not Test 
            //locationID = this.ODMUser.StationID;
            locationID = this.ODMUser.LocationID;

            staffID = this.ODMUserID;
            staffName = this.ODMUser.Name;

            if (String.IsNullOrEmpty(this.txtHideReason.Text.Trim()))
            {
                eventTime = ConvertToDateTime(this.txtRegDate.Text + " " + this.txtRegTime.Text + ":00");
            }
            else
            {
                eventTime = ConvertToDateTime(this.txtHideRegDate.Text + " " + this.txtHideRegTime.Text + ":00");
            }

            int shiftID = int.Parse(this.dllShift.SelectedValue);
            int actionType = 1;
            string amendTimeReason = this.txtHideReason.Text;

            StationEventID = BLL.SOM.Event.StationEventSCBookOn.Save(locationID, eventTime, shiftID, this.ODMUser.StaffInfoId, this.ODMUser.ID, actionType, amendTimeReason, this.LangCode);
            //SetPageLoad();
            DataTable dtOneStationEvent = MTRC.ODMS.DAL.SOM.Event.StationEventDa.GetOneStationEventByStationEventID(StationEventID);
            this.lblRegNoValue.Text = dtOneStationEvent.Rows[0]["EventNo"].ToString();
            this.IsServerChanged = false;
            this.btnSave.Enabled = false;
            this.btnDelete.Enabled = true;
            this.dllShift.Enabled = false;
            if (this.txtHideReason.Text.Trim() != "")
            {
                this.txtRegDate.Text = this.txtHideRegDate.Text;
                this.txtRegTime.Text = this.txtHideRegTime.Text;
            }
            this.imgStatus.ImageUrl = "../../Images/SOM/EventStatus_COM.gif";
            this.imgStatus.ToolTip = this.GetLabel("SOM_Completed");

            //this.btnRegDateTime.Attributes.Add("onclick", "javascript:window.showModalDialog('../Popup/SetRegEventTime.aspx?PageID=DutySCEntry&StationEventID=" + StationEventID + "',window,'dialogHeight:500px;dialogWidth:500px;center:yes;resizable:yes;scroll:no;status:no;unadorne:yes'); return false;");

            #region Add By Nathan 2015/06/15
            string strURL = "";
            if (Request.QueryString["isBack"] != null)
            {
                strURL = "DutySCRegisterEntry.aspx?Action=New&StationEventID=" + this.StationEventID + "&isBack=" + Request.QueryString["isBack"] + "&PageID=" + Request.QueryString["PageID"];
            }
            else
            {
                strURL = "DutySCRegisterEntry.aspx?Action=New&StationEventID=" + this.StationEventID;
            }
            #endregion Add By Nathan 2015/06/15
            //string strURL = "DutySCRegisterEntry.aspx?Action=New&StationEventID=" + this.StationEventID;
            string addScript = "location.href='" + strURL + "'";
            this.PAlert(this.GetMessage("COM_UpdateResultSuccessfully"), 0, addScript);

            //this.Alert(this.GetMessage("COM_UpdateResultSuccessfully"));
            dtOneStationEvent.Dispose();
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            btnRegDateTime.Attributes.Add("style", "background-color: inherit;");
            this.Response.Redirect("DutySCRegisterEntry.aspx?Action=Edit");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            //MTRC.ODMS.DAL.SOM.Event.StationEventDa.Update(StationEventID, 0, 0, string.Empty, DateTime.Now, 0, null, DateTime.Now, 0, string.Empty, string.Empty, string.Empty, 0, 0, 0, 3, false, string.Empty, 0, 0, DateTime.Now, true, "", -1, 0, DateTime.Now, 0, 3);

            MTRC.ODMS.Model.SOM.Event.StationEventInfo stationEventModel = new MTRC.ODMS.Model.SOM.Event.StationEventInfo();
            stationEventModel.StationEventID = this.StationEventID;
            //update by gordon 20151020 U247 Can not Test
            //stationEventModel.LocationID = this.ODMUser.StationID;
            stationEventModel.LocationID = this.ODMUser.LocationID;
            stationEventModel.EventStatusID = 3;
            stationEventModel.LastUpdUser = this.ODMUser.ID;
            stationEventModel.LastUpdTime = MTRC.ODMS.DAL.COM.Util.GetCurrentSystemTime();

            MTRC.ODMS.DAL.SOM.Event.StationEventDa.DeleteEvent(stationEventModel);

            this.dllShift.Enabled = false;
            this.btnDelete.Enabled = false;
            this.imgStatus.ImageUrl = "../../Images/SOM/EventStatus_Void.gif";
            this.imgStatus.ToolTip = this.GetLabel("SOM_Voided");
            this.Alert(this.GetMessage("SOM_VoidOperate"));
        }


    }
}
